//
//  PYParamsters.m
//  Piaoyoung
//
//  Created by LiTengFei on 15/6/2.
//  Copyright (c) 2015年 HangZhou PiaoYoung Co.Ltd. All rights reserved.
//

#import "ParameterCreator.h"
#import <CommonCrypto/CommonCrypto.h>

typedef  NS_ENUM(NSInteger,ParamsType) {
    ParamsTypeNone = 0,
    ParamsTypeSec = 1
};

static NSString* md5Transfer(NSString * string){
    const char *original_str = [string UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(original_str, (CC_LONG) strlen(original_str), result);
    NSMutableString *hash = [NSMutableString string];
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [hash appendFormat:@"%02X", result[i]];
    }
    return [hash lowercaseString];
}

@interface ParameterCreator ()

@property(nonatomic, strong) NSMutableDictionary *defaultParams;

@end

@implementation ParameterCreator

- (instancetype)init {
    self = [super init];
    if (self) {
        self.defaultParams = [NSMutableDictionary dictionary];
    }
    return self;
}

+ (instancetype)defaultCreator {
    ParameterCreator *creator = [[ParameterCreator alloc] init];
    creator.paramsType = ParamsTypeSec;
    return creator;
}

+ (instancetype)creatorWithDictionary:(NSDictionary *)dictionaty {
    ParameterCreator *creator = [ParameterCreator defaultCreator];
    for (NSString *key in dictionaty.allKeys) {
        [creator addKey:key value:dictionaty[key]];
    }
    return creator;
}

-(instancetype)addKey:(NSString *)key value:(NSObject *)value md5:(BOOL)enableMD5{
    if (key == nil || key.length==0) {
        return self;
    }
    if (value == nil) {
        return self;
    }
    if([value isKindOfClass:[NSNull class]]){
        return self;
    }
    NSParameterAssert(value);
    if ([value isKindOfClass:[NSString class]] && ((NSString *) value).length == 0) {
        return self;
    }
    
    key = [key stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    if(enableMD5){
        if([value isKindOfClass:[NSString class]]){
            NSString * valueString = (NSString *)value;
            _defaultParams[key] = md5Transfer(valueString);
        }
    }else{
        _defaultParams[key] = value;
    }
    return self;
}

- (instancetype)addKey:(NSString *)key value:(NSObject *)value {
    return [self addKey:key value:value md5:NO];
}

- (NSDictionary *)params {
    NSLog(@"%@",_defaultParams);
    if(self.paramsType==ParamsTypeSec){
        NSMutableDictionary * dictionary = [[NSMutableDictionary alloc]init];
        NSError * error;
        NSDictionary * dict = self.defaultParams;
        
        NSData *dictData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
        
        NSString * string = [dictData base64EncodedStringWithOptions:(NSDataBase64EncodingOptions) 0];
        
        dictionary[@"obj"] = string;
        
        return dictionary;
    }else{
        return [self.defaultParams copy];
    }
}

- (NSString *)paramsString {
    NSError *error;
    NSData *data = [NSJSONSerialization dataWithJSONObject:self.params options:NSJSONWritingPrettyPrinted error:&error];
    if (error) {
        NSLog(@"%@", error);
    }
    NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    return string;
}

-(NSDictionary *)originParams{
    return self.defaultParams;
}




@end


